package org.itboys.mobile.service.mysql.web.order;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.order.MdlOrderProductMapper;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.order.MdlOrder;
import org.itboys.mobile.entity.mysql.order.MdlOrderProduct;
import org.itboys.mobile.service.mongo.web.member.SundayMemberService;
import org.itboys.mobile.service.mysql.mobile.income.MdlMobileIncomeService;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/5
 * 描述：迈迪龙_后台_订单
 */
@Service
public class MdlOrderService extends BaseMySqlService<MdlOrder>{
    @Autowired
    private MdlOrderProductMapper orderProductMapper;
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private MdlMobileIncomeService incomeService;
    @Autowired
    private SundayMemberService memberService;
    /**
     * 分页获取订单
     * @param request
     * @param type
     * @return
     */
    public PageResult<MdlOrder> selectOrder(HttpServletRequest request, Integer type){
        Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"orderNo","memberName","shopName","orderStatus","userType");
        param.put("type",type);
        return  new PageResult<MdlOrder>(super.select(param),super.count(param));
    }
    /**
     * 查询订单详情
     * @param orderId
     * @return
     */
    public MdlOrder findOne(Long orderId,boolean isPackageProduct){
        MdlOrder order = super.find(orderId);
        if(isPackageProduct){
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("orderId",orderId);
            order.setProducts(orderProductMapper.select(param));
        }
        return order;
    }
    /**
     * 分页获取订单商品
     * @param request
     * @param orderId
     * @return
     */
    public PageResult<MdlOrderProduct> selectProduct(HttpServletRequest request, Long orderId ){
        Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"");
        param.put("orderId",orderId);
        Serializable count = orderProductMapper.count(param);
        Long countResult = count==null?0l: Long.valueOf(count.toString());
        return  new PageResult<MdlOrderProduct>(orderProductMapper.select(param),countResult);
    }

    /**
     * 确认订单
     * @param orderId
     */
    public void confirmOrder(Long orderId,String  confirmMessage){
        MdlOrder order=super.find(orderId);
    /*    if(order.getType()==SundayCommonConstants.type_caigou){


        }
        if(order.getType()==SundayCommonConstants.type_mall){

            SundayMember member=memberService.getById(order.getMemberId());
            //生成销售员收益
            if(LongUtil.isNotZreo(member.getRecommenderId())){
                SundayMember salesMan=memberService.getById(member.getRecommenderId());
                incomeService.updateIncome(salesMan.getId(),order.getPayAmount(),order,SundayCommonConstants.INCOME_TYPE_BUSINESS);
            }
            //生成代理商收益
            incomeService.updateIncome(member.getAgentId(),order.getPayAmount(),order,SundayCommonConstants.INCOME_TYPE_BUSINESS);
        }*/
        order.setOrderStatus(SundayCommonConstants.ORDER_STATUS_DPS);
        order.setConfirmTime(new Date());
        order.setConfirmMessage(confirmMessage);
        super.update(order);
    }
    /**
     * 配送采购订单
     * @param orderId
     * @return
     */
    public void sendOrder(Long orderId) {

        MdlOrder order = super.find(orderId);
        if(order.getOrderStatus()== SundayCommonConstants.ORDER_STATUS_DPS){
            order.setOrderStatus(SundayCommonConstants.ORDER_STATUS_PSZ);
            order.setSendTime(new Date());
            super.update(order);
        }
    }


}
